package com.heraleign.boot.controller;

import com.heraleign.boot.bean.Account;
import com.heraleign.boot.service.AccountService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = {"帐户管理"})  // 用在类上
public class AccountController {

    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping("/account")
    @ApiOperation(value = "显示所有帐户信息")  // 用在方法上
    public List<Account> getAccounts(){
        logger.info("从数据库读取Student集合");
        return accountService.getList();
    }

    @ResponseBody
    @GetMapping("/getAccountById")
    @ApiOperation(value = "根据帐户ID查找帐户信息")  // 用在方法上
    @ApiImplicitParams(
        @ApiImplicitParam(
            name = "acctId" ,
            value ="帐户ID" ,
            required = true ,
            paramType = "path",
            dataType = "Integer",
            dataTypeClass = Integer.class
        )
    )
    public Account getAcctById(@RequestParam("accId") String accId){
        System.out.println(accId);
        //Account account =accountService.getAcctById("A0002");
        // http://localhost:8080/getAccountById?accId=A0002
        // http://localhost:8080/getAccountById?accId="A0002" 参数不能带引号，带了引号是请求不到数据的
        Account account =accountService.getAcctById(accId);
        System.out.println(account.getAccName());
        return account;
    }

    @ResponseBody
    @GetMapping("/getAccountById2/{accId}")
    public Account getAcctById2(@PathVariable String accId){
        System.out.println(accId);
        //Account account =accountService.getAcctById("A0002");
        // http://localhost:8080/getAccountById?accId=A0002
        // http://localhost:8080/getAccountById?accId="A0002" 参数不能带引号，带了引号是请求不到数据的
        Account account =accountService.getAcctById(accId);
        System.out.println(account.getAccName());
        return account;
    }

    @PostMapping()
    @ApiOperation("新增帐户")
    public void saveAcct(@ApiParam(name="acct",value="帐户对象" ) @RequestBody Account acct){
        System.out.println("保存帐户成功"+acct);
    }

}
